package com.youngjun.common.oss.autoconfigure;

import com.youngjun.common.oss.expection.OssAutoConfigurationException;
import com.youngjun.common.oss.manager.impl.MinioOssManagerImpl;
import com.youngjun.common.oss.manager.OssManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author VirgoJun
 */
@Configuration
@EnableConfigurationProperties(OssProperties.class)
public class OssAutoConfiguration {

    @Autowired
    private OssProperties ossProperties;

    @Bean
    OssManager ossManager() {
        if(ossProperties.getProvider() == null){
            throw new OssAutoConfigurationException("oss provider cannot be empty");
        }
        if(ossProperties.getEndpoint() == null){
            throw new OssAutoConfigurationException("oss endpoint cannot be empty");
        }
        if(ossProperties.getAccessKey() == null){
            throw new OssAutoConfigurationException("oss accessKey cannot be empty");
        }
        if(ossProperties.getSecretKey() == null){
            throw new OssAutoConfigurationException("oss secretKey cannot be empty");
        }
        switch (ossProperties.getProvider()){
            case MINIO:
                return new MinioOssManagerImpl(ossProperties.getEndpoint(), ossProperties.getAccessKey(), ossProperties.getSecretKey());
            default:
                throw new OssAutoConfigurationException("oss properties cannot be empty");
        }
    }
}
